package com.bytenebula.data;

import com.bytenebula.entity.BnJdbcDriver;
import com.bytenebula.entity.meta.MetaDatabase;

/**
 * 生成JdbcUrl链接字符串
 */
public abstract class JdbcUrlBuilder {

    /**
     * 根据Jdbc模板生成JDBC URL
     * @param database 数据库对象
     * @return 返回Jdbc URL
     */
    public String buildJdbcUrl(MetaDatabase database, BnJdbcDriver driver) {
        String url_tpl = driver.getUrlTemplate();
        String url = url_tpl.replace("{host}", database.getHostName());
        url = url.replace("{port}", database.getPort().toString());
        url = url.replace("{database}", database.getDbName());
        return this.buildParams(url, database);
    }

    /**
     * 处理JDBC URL扩展参数
     * @param url JDBC URL
     * @param database 数据库对象
     * @return 返回Jdbc URL参数字符串
     */
    protected abstract String buildParams(String url, MetaDatabase database);

}
